/*
 Copyright (c) 2013, Ike Yousuf <admin@kudodev.com>
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 names of its contributors may be used to endorse or promote products
 derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.kudo.gdx.ui.components;

import com.kudo.gdx.Graphics;
import com.kudo.gdx.ui.Component;
import com.kudo.gdx.ui.Container;
import com.kudo.gdx.ui.Skin;
import com.kudo.gdx.ui.Skin.Padding;
import com.kudo.gdx.ui.event.MouseListener;

/**
 *
 * @author Ike
 */
public class Frame extends Container {

    private TitleBar titleBar = null;
    private ResizeButton resizeButton = null;
    private boolean resizeable = true;
    private boolean draggable = true;
    private String activeFrameImg = "activeBlue", inactiveFrameImg = "inactiveBlue";

    public Frame() {
        titleBar = new TitleBar(this);
        add(titleBar);
        resizeButton = new ResizeButton(this);
        add(resizeButton);
    }

    public Frame(int x, int y, int w, int h) {
        super(x, y, w, h);
        titleBar = new TitleBar(this);
        add(titleBar);
        resizeButton = new ResizeButton(this);
        add(resizeButton);
    }

    public ResizeButton getResizeButton() {
        return resizeButton;
    }

    public void pack() {
        float w = 0, h = 0;
        for (Component c : getChildren()) {
            if (c != resizeButton && c != titleBar) {
                if (w < c.getWidth() + c.getRelativeX() + c.getRelativeX() + 10) {
                    w = c.getWidth() + c.getRelativeX() + c.getRelativeX() + 10;
                    setWidth(w);
                    titleBar.setWidth(w);
                }
                if (h < c.getHeight() + c.getRelativeY()) {
                    h = c.getHeight() + c.getRelativeY();
                    setHeight(h + 5);
                }
            }
        }
        resizeButton.setLocation(getWidth() - 15, getHeight() - 15);
    }

    @Override
    public void setText(String t) {
        titleBar.setTitle(t);
    }

    @Override
    public void setWidth(float i) {
        getBounds().width = i;
        resizeButton.setLocation(getWidth() - 15, getHeight() - 15);
    }

    @Override
    public void setHeight(float i) {
        getBounds().height = i;
        resizeButton.setLocation(getWidth() - 15, getHeight() - 15);
    }

    public void setDraggable(boolean b) {
        draggable = b;
    }

    public void setResizable(boolean b) {
        resizeable = b;
    }

    @Override
    public String getText() {
        return titleBar.getTitle();
    }

    @Override
    public void render(Graphics g) {
        if (!getVisible()) {
            return;
        }

        if (getActive()) {
            g.draw(Skin.instance().getFrame(activeFrameImg), getBounds().x, getBounds().y + 20, getBounds().width, getBounds().height - 20);
        } else {
            g.draw(Skin.instance().getFrame(inactiveFrameImg), getBounds().x, getBounds().y + 20, getBounds().width, getBounds().height - 20);
        }

        g.beginClipping(getBounds());
        renderChildren(g);
        g.endClipping();
    }

    public class ResizeButton extends Button {

        public ResizeButton(Frame f) {
            super(f.getWidth() - 15, f.getHeight() - 15, 10, 10);
            getInput().addProcessor(0, new ButtonListener());
        }

        @Override
        public void render(Graphics g) {
            if (!getVisible()) {
                return;
            }
            if (((Frame) getParent()).resizeable) {
                g.draw(Skin.instance().getResizeButton(), getBounds().x, getBounds().y);
            }
        }

        private class ButtonListener extends MouseListener {

            private int lastX, lastY;
            private boolean dragging = false;

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                if (contains(screenX, screenY) && ((Frame) getParent()).resizeable) {
                    lastX = screenX;
                    lastY = screenY;
                    dragging = true;
                    return true;
                }
                return false;
            }

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {
                if (dragging) {
                    dragging = false;
                    return true;
                }
                return false;
            }

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                if (dragging) {
                    float w = getParent().getBounds().width - (lastX - screenX);
                    float h = getParent().getBounds().height - (lastY - screenY);
                    if (w >= getParent().getMinimalWidth()) {
                        getParent().getBounds().width = w;
                        titleBar.setWidth(w);
                        lastX = screenX;
                    }
                    if (h >= getParent().getMinimalHeight()) {
                        getParent().getBounds().height = h;
                        lastY = screenY;
                    }

                    setLocation(getParent().getWidth() - 15, getParent().getHeight() - 15);

                    return true;
                }
                return false;
            }
        }
    }

    public class TitleBar extends Container {

        private Label title = null;

        public TitleBar(Frame f) {
            super(0, 0);
            setSize(f.getWidth(), 24f);

            title = new Label("");
            title.setLocation(7, 5);
            add(title);
            getInput().addProcessor(0, new TitleListener());
            setHasBorder(false);
        }

        public void setBoundsLocation(float x, float y) {
            if (getParent() != null) {
                getParent().setBoundsLocation(x, y);
            }
            updateChildren();
        }

        @Override
        public void render(Graphics g) {
            if (((Frame) parent).getActive()) {
                g.draw(Skin.instance().getButton("normalBlue"), parent.getX(), parent.getY(), parent.getWidth(), 24);
            } else {
                g.draw(Skin.instance().getButton("downBlue"), parent.getX(), parent.getY(), parent.getWidth(), 24);
            }

            renderChildren(g);
        }

        public void setTitle(String t) {
            title.setText(t);
        }

        public String getTitle() {
            return title.getText();
        }

        private class TitleListener extends MouseListener {

            private int lastX, lastY;
            private boolean dragging = false;

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                if (contains(screenX, screenY) && ((Frame) getParent()).draggable) {
                    lastX = screenX;
                    lastY = screenY;
                    dragging = true;
                    return true;
                }
                return false;
            }

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {
                if (dragging) {
                    dragging = false;
                    return true;
                }
                return false;
            }

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                if (dragging) {
                    setBoundsLocation(getX() - (lastX - screenX), getY() - (lastY - screenY));

                    lastX = screenX;
                    lastY = screenY;
                    return true;
                }
                return false;
            }
        }
    }
}
